import 'package:bxultimate/domain/common/AreaXzqh.dart';
import 'package:bxultimate/domain/common/MapLatLonDto.dart';
import 'package:bxultimate/domain/common/UploadImageDto.dart';
import 'package:bxultimate/domain/common/dict_data.dart';
import 'package:bxultimate/shared/shared.dart';
import 'package:dio/dio.dart';

class BaseRequest {
  /**
   * 获取文件基本路径
   */
  static Future<String> getFilePrefix() async {
    var response = await UtilHttp().get(
      '/common/sysConfig/selectByConfigKey/base_file_url',
    );
    return response['data']['configValue'];
  }

  // 获取字典数据
  static Future<List<DictData>> getDictData(code) async {
    var response = await UtilHttp().get(
      '/common/sysDictData/' + code,
    );
    return response['data']
        .map<DictData>((item) => DictData.fromJson(item))
        .toList();
  }

  static Future<DictData> selectByDicTypeAndValue(code,value) async {
    var response = await UtilHttp().get(
      '/common/sysDictData/selectByDicTypeAndValue?dictType=${code}&dictValue=${value}',
    );
    return DictData.fromJson(response['data']);
  }

  // 获取行政区划
  static Future<List<AreaXzqh>> getAreaXzqhByCode(code) async {
    var response = await UtilHttp().get(
      '/common/sysAreaXzqh/getByParentCode/${code}' ,
    );
    return response['data']
        .map<AreaXzqh>((item) => AreaXzqh.fromJson(item))
        .toList();
  }

  // 文件上传
  static Future<UploadImageDto> uploadFile(path) async {
    var name = path.substring(path.lastIndexOf("/") + 1, path.length);
    String imageType = name.substring(name.lastIndexOf(".") + 1, name.length);
    var image = await MultipartFile.fromFile(
      path,
      filename: name + "." + imageType,
    );
    var response = await UtilHttp().postForm('/common/ignore/files-anon',
        data: {"file": image, "btype": "business", "ftype": ""});
    return UploadImageDto.fromJson(response['data']);
  }

  static Future<MapLatLonDto> getLatLonAddress(lat, lon) async {
    var response = await UtilHttp().get(
      '/map/poi/getDataByLonLat?lat=' +
          lat.toString() +
          '&lon=' +
          lon.toString() +
          '&page=0&size=1&focusLon=' +
          lon.toString() +
          '&focusLat=' +
          lat.toString(),
    );
    return MapLatLonDto.fromJson(response['data']['data'][0]);
  }


  static Future<UploadImageDto> uploadWithBtype(path, btype,ftype) async {
    var name = path.substring(path.lastIndexOf("/") + 1, path.length);
    String imageType = name.substring(name.lastIndexOf(".") + 1, name.length);
    var image = await MultipartFile.fromFile(
      path,
      filename: name + "." + imageType,
    );
    var response = await UtilHttp().postForm('/common/ignore/files-anon',
        data: {"file": image, "btype": btype, "ftype": btype});
    return UploadImageDto.fromJson(response['data']);
  }

  static Future downloadFile(url, path) async {
   return  await UtilHttp().download(url, path);
  }

}
